package com.itheima.bookdao;

import com.itheima.domain.Book;

public class BookDao implements BaseBookDao {

    private static Book[] books = new Book[5];

    public Book[] getAllBooks() {
        return books;
    }

    public boolean addBook(Book book) {
        int index = -1;
        for (int i = 0; i < books.length; i++) {
            Book bookArr = books[i];
            if(bookArr == null) {
                index = i;
                break;
            }
        }

        if(index != -1) {
            books[index] = book;
            return true;
        }

        return false;
    }

    public void deleteBook(String bookId) {
        int index = -1;
        for (int i = 0; i < books.length; i++) {
            Book bookArr = books[i];
            if(bookArr != null){
                if(bookArr.getBookId().equals(bookId)){
                    index = i;
                    break;
                }
            }
        }

        if(index != -1){
            books[index] = null;
        }
    }

    public void updateBook(Book book) {
        int index = -1;
        for (int i = 0; i < books.length; i++) {
            Book bookArr = books[i];
            if(bookArr != null){
                if(bookArr.getBookId().equals(book.getBookId())){
                    index = i;
                    break;
                }
            }
        }

        if(index != -1){
            books[index] = book;
        }
    }
}
